package cn.xorange.love.controller;

import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.utils.SecretUtils;
import cn.xorange.commons.file.service.QiNiuUploadService;
import cn.xorange.commons.file.utils.FileUtil;
import cn.xorange.commons.security.annotation.PassToken;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.utils.cache.ICacheService;
import cn.xorange.commons.utils.io.FileUtils;
import cn.xorange.commons.utils.lang.StringUtils;
import cn.xorange.commons.utils.thirdApp.WeChatUtils;
import cn.xorange.love.common.XoCacheKey;
import cn.xorange.love.common.XoConstants;
import cn.xorange.love.common.XoUtils;
import cn.xorange.love.entity.AppUserCert;
import cn.xorange.love.service.AppUserCertService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : yangjian
 * date : 2024/7/4
 * description :
 */
@RestController
@RequestMapping("/app/auth")
public class AppUserCertController {

    @Resource
    ICacheService cacheService;
    @Resource
    AppUserCertService service;
    @Resource
    QiNiuUploadService uploadService;


    @GetMapping("/get/{type}")
    public ResultBean getAuth(@PathVariable("type") String type){
        Long userId = SecurityUtils.getUserId();
        AppUserCert userAuth = new AppUserCert();
        userAuth.setUserId(userId);
        userAuth.setType(type);
        userAuth.order.desc("createTime");
        userAuth.setSelectLimit(1);
        userAuth = service.getEntity(userAuth);

        return ResultBean.onSuccess(userAuth);
    }

    @PassToken
    @PostMapping("/ocrIdCard")
    public ResultBean authOcrIdCard(@RequestParam(value = "file") final MultipartFile file){

        File tempFile = FileUtils.multipartFileToFile(file);
        String[] appidAndSecret = SecretUtils.getIdAndSecret(XoCacheKey.MP_LOVE);
        Long orangeId = XoUtils.getOrangeId();

        XoUtils.checkCertAuthAttack(XoConstants.AUTH_TYPE_ID_CARD);

        String token = WeChatUtils.getToken(orangeId, appidAndSecret[0], appidAndSecret[1]);
        String res = WeChatUtils.ocrIdCard(token,"Front", tempFile);
        JSONObject obj = JSON.parseObject(res);
        Map<String, String> map = new HashMap<>();
        String birth = obj.getString("birth");
        String id = obj.getString("id");
        map.put("id", id);
        map.put("birthday", birth);
        map.put("name", obj.getString("name"));
        map.put("addr", obj.getString("addr"));
        map.put("sex",obj.getString("gender"));
        map.put("nationality", obj.getString("nationality"));

        if(Objects.nonNull(id)&&id.length()!=18){
            return ResultBean.onFail("认证失败");
        }

        String br = birth.split("-")[0].substring(2);
        String path = StringUtils.format("{}/{}",br,orangeId);

        String filePath = uploadService.uploadFile(path, tempFile);
        tempFile.delete();
        map.put("pic", filePath);

        service.addAuth(XoConstants.AUTH_TYPE_ID_CARD, map);

        return ResultBean.onSuccess(map);
    }


}
